import { createRouter, createWebHistory } from 'vue-router'
import layout from '@/views/layout/index.vue'
import login from '@/views/login/index.vue'
import home from '@/views/home/index.vue'
import category from '@/views/category/index.vue'
import subCategory from '@/views/subCategory/index.vue'
import details from '@/views/details/index.vue'
import shoppingCart from '@/views/shoppingCart/index.vue'
import order from '@/views/order/index.vue'
import payPage from '@/views/order/payPage.vue'
import payResult from '@/views/order/payResult.vue'
import member from '@/views/member/index.vue'
import user from '@/views/member/components/user.vue'
import myOrder from '@/views/member/components/order.vue'

import nProgress from 'nprogress'
import 'nprogress/nprogress.css'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: layout,
      children: [
        {
          path: '',
          component: home
        },
        {
          path: '/category/:id',
          component: category
        },
        {
          path: '/category/sub/:id',
          component: subCategory
        },
        {
          path: '/details/:id',
          component: details
        },
        {
          path: '/shopping-cart',
          component: shoppingCart
        },
        {
          path: '/order',
          component: order
        },
        {
          path: '/payPage/:id',
          component: payPage
        },
        {
          path: '/payResult',
          component: payResult
        },
        {
          path: '/member',
          component: member,
          children: [
            {
              path: '',
              component: user
            },
            {
              path: '/member/order',
              component: myOrder
            }
          ]
        }
      ]
    },
    {
      path: '/login',
      component: login
    }
  ],
  // 定制路由行为
  scrollBehavior() {
    return {
      top: 0,
      behavior: 'smooth' // 可以使滚动平滑进行，可选
    }
  }
})

router.beforeEach(() => {
  nProgress.start()
})

router.afterEach(() => {
  // 关闭进度条
  nProgress.done()
})

export default router
